#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>

#include "goose_receiver.h"
#include "goose_subscriber.h"
#include "hal_thread.h"
#include "linked_list.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    GetNetInterface();
    m_gooseSub = new Goose61850Subscriber();
    m_gooseSub->setInterface(ui->cb_netInterface->currentText());
    connect(ui->cb_netInterface, &QComboBox::currentTextChanged, this, [this](const QString &arg1){
       if(m_gooseSub!=NULL) m_gooseSub->setInterface(arg1);
       qDebug() << arg1;
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::GetNetInterface()
{
    ui->cb_netInterface->clear();
    FILE* f = fopen("/proc/net/dev","r");
    char szLine[512];
    if (!f)
    {
        perror("socket");
        exit(1);
    }
    fgets(szLine, sizeof(szLine), f);
    fgets(szLine, sizeof(szLine), f);
    while(fgets(szLine, sizeof(szLine), f))
    {
        char szName[128] = {0};
        sscanf(szLine, "%s", szName);
        int nLen = strlen(szName);
        if (nLen <= 0)continue;
        if (szName[nLen - 1] == ',') szName[nLen - 1] = 0;
        if (strcmp(szName, "lo") == 0)continue;
        QString str = QString(szName);
        str = str.mid(0, str.size()-1);
        ui->cb_netInterface->addItem(str);
//        nCount++;
//        qDebug() << str.mid(0, str.size()-1);
    }
    ui->cb_netInterface->setCurrentText("lo");
}
